package edu.gatech.oad.mule;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

import edu.gatech.oad.mule.config.interfaces._ConfigurationPanel;
import edu.gatech.oad.mule.model.GameStateMachine.GameState;
import edu.gatech.oad.mule.view.interfaces.RenderableBuilding;

/**
 * This class represents the town's store.  Here, you can buy/sell various
 * resources you possess
 *
 * @author Brendan McGarry, Dale Avery, D'Miria Collins, Natasha Parekh, Jaemin Park
 * @version %I%, %G%
 */
public class Store extends Zone implements RenderableBuilding{
	
	private Player player;
	private StoreGUI sg;
	private Wallet w;
	
	private Timer timer;
	
	/**
	 * This constructor creates the new store object
	 * 
	 * @param x The x position of the store
	 * @param y The y position of the store
	 * @param width The width of the store
	 * @param height The height of the store
	 */
	public Store(int x, int y, int width, int height, _ConfigurationPanel cp){
		super(x, y, width, height);
		if(cp.getDifficulty() == "Beginner"){
			w = new Wallet(0, 16, 16, 0, 0);
		}//end if
		else{
			w = new Wallet(0, 8, 8, 0, 8);
		}//end else
		createStoreGUI();
	}//end constructor
	
	//TODO delete after junit tests
	public void setSmithore(int amount) {
		w.setSmithore(amount);
	}
	
	/**
	 * This method creates the store GUI for the 
	 * players to interact with
	 */
	private void createStoreGUI(){
		SwingUtilities.invokeLater(new Runnable(){
			@Override
			public void run(){
				sg = new StoreGUI(w, null);
			}//end method run
		});	
	}//end method createStoreGUI

	/**
	 * @return The amount of smithore the Town Store has
	 */
	public int getSmithore(){
		return w.getSmithore();
	}//end method getSmithore
	
	/**
	 * Adds a specified amount of smithore to the Town store
	 * 
	 * @param amount The amount of smithore to add
	 */
	public void addSmithore(int amount){
		w.addSmithore(amount);
	}//end method addSmithore
	
	//====================
	//	RenderableBuilding
	//====================
	
	@Override
	public BuildingType getBuildingType() {
		return BuildingType.STORE;
	}//end method getBuildingType

	@Override
	public int getCost() {
		return 0;
	}//end method getCost
	
	//====================
	//	Zone
	//====================
	
	@Override
	public void interact(Player p, GameState phase, int round, int time) {
		System.out.println("Time Left: " + time + "ms");
		System.out.print(p.getName() + " enter's the town store with [" + p.getMoney() + "] money, ");
		System.out.print("[" + p.getCrystite() + "] crystite, ");
		System.out.print("[" + p.getFood() + "] food, ");
		System.out.print("[" + p.getEnergy() + "] energy, and ");
		System.out.print("[" + p.getSmithore() + "] smithore. ");
		//set the panel interacting with this panel
		sg.setPlayer(p);
		//this is the listener for the timer. when it goes off, the player's turn is over
		//  and we need to make the store GUI go away
		ActionListener timerDone = new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				sg.setVisible(false);
			}//end method actionPerformed
		};
		//create the timer object and give it the action listener above
		timer = new Timer(time, timerDone);
		//don't repeat the timer
		timer.setRepeats(false);
		//start the timer
		timer.start();
		//show the GUI
		sg.showGUI();
	}//end method interact

}//end class store
